package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.MonthCommentAdapter;
import com.approval.approvalapp.center.adapter.ThingsProductAdapter;
import com.approval.approvalapp.center.bean.ThingsProductBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

//办件效率统计
public class ThingsProductivityActivity extends BaseActivity {

    public static void goInto(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ThingsProductivityActivity.class);
        context.startActivity(intent);
    }
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private ThingsProductAdapter mAdapter;
    List<ThingsProductBean> mList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.things_productivity_layout;
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ThingsProductAdapter(mList,this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData();
                    }
                }, 500);
            }
        });
        getData();
    }

    public void getData() {
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.ORG_CODE, "-1"));
        jsonObject.put("region_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/hao_list", jsonObject.toString(), new NetDownResponse() {
            public String TAG = getClass().getSimpleName();

            @Override
            public void success(String str) {
                Gson gson = new Gson();
                Type type = new TypeToken<List<ThingsProductBean>>() {
                }.getType();
                List<ThingsProductBean> list = gson.fromJson(str, type);

                mList.clear();
                mList.addAll(list);
                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                if (swipeRefreshLayout != null) {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
